package com.msb.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;

import com.msb.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 支付宝的配置文件
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 商户appid
    public static String APPID = "2021000147689131";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCdfet8eICygE2tsw2NeSsOQGGfJkXzrkzX7FWK82NP7gNp7SuMx5vBY7r6QX972Q2xlZzp5Nz5Xd69YMT6cWQlZrK1W9v/IZDk93yL+XnasxJCO2LClUlbHlUu71SSMvDcupRar+iDussFXkGxGLF7aQRN3UEt6Y+b9JaXBMtgqdobraO+Ki+H4CZ6G4xMyDr7h3VFkBOQOtyRiMRopRvJWwLj9sVtIIfuDUzgKcQS27tTSxq5dd00bMwFyFDxfCBuDanDC3H8W0tEst8MSbCfn3YjsM9UQhbQ0T3oJnsYVQI5JU8DA8G4bbtK7x9jSqh+7NpbkWxMM05Rqcx1XAGHAgMBAAECggEAQgjlLyh7ldy0NUupOBOBGRWnmQp8NCN2NubyKzPIBWCf788w/nNHfHvrjzjdUjl9Q2OfCncarDhOx4sGrJfUzNXVwrNEeCJD1a1rfGlbTXbeBgVOcJYVzu3V+7r/89kGIxbcU3YEgQ9WRubxlmbrK/qpglhuFukifNq+NY4eFGYZfSeB6qoey5lwvh9EAc7QLEJLbwF7ZqOdM9Jt1NAHbFng4ngRiFcII+ay9AGXMGEPH1pcH2XvIJJmgX3rsN1Z3AwKqtXWtFXpmVHO+W5Fneku6w4ddxEyAj3aPx/fIjoWkCqrHSUgPG+/i7QSIqsRip2vWCMLM6Vr2HEKB/9veQKBgQDmljcnC5CRwUP+I3aW8E/ABwjlZyECZts9qvhDhzM/QHSSIRvwQAMJcqU9V06Zh+k0yk/0zX5Ae25wXmLGEFm80o74XtJTTIj9AnyBUP2prP6WqtGa0EnANHyQxezJk/8/Zz24nIiLDwKqXpCvXGTRdazcaaRJNTTNbQd1HVKxFQKBgQCu2WbhvVdfsftRxY3AJXgsTDO8shbpzw/k46SCMSVt2s4wLZAlCk36/mc0d3612SeTntnbXc0ppOcmj9T9lSHhF81L4h4bbw8OqgAbFzCSZBoFwA6EbZ+oGtFz+Dxy2I0qnAIbrf/kMIM96DVHtXaEyUrHz2pcwpCdQL6aJmn3KwKBgDox1yGHjaj7ecl98cQOSlJQdDJRYMdWZiaeK6H/UTO5dLIcoQx1W07bHFDq4+9jmbu1A5Do++ASRsEdURhjsAIzsdmFNNfJDRmtsvaAFkToYChuXBASWUqk9HslOKl3LIg36h/Zgax20VlmHGXlj5w9cq7+CnRfdUATMDqid+gxAoGBAIP/1E+vZXr1HspI3g6lOugWTyixOeMhUtp1kQhImf0pJmfwvdV4xg4qaTeOzrXyTpJ/ZfDaC7zYpTjTdPRL1dvc4DvdZOVJHpDvfkz1YNHjiFG3AD6xk8qRFHb8fIYnPcthI6ZJ4dkfVrUxzKdqMyNj43Sc/asxp4NQEZTGU1EpAoGAB2Xhe7Ckix1G3ohoQqKPpJ//oeNIItJtsr/2yre7RhAGGfwEg68/tDkaYkaF8cQM2hy1aBiAYABsgIPJiorx2p2UM9MQ4eEhvidmZwFp152U7MrUJVz6YRpSoSvXqGMjqtjXhDr3JDpPS/+DLLoXsZZZQDKQfYfpLgmcNRHTL8Y=";
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://order.wlp.com/payed/notify";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url = "http://order.wlp.com/orderPay/returnUrl";
    // 请求网关地址
    public static String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnX3rfHiAsoBNrbMNjXkrDkBhnyZF865M1+xVivNjT+4Dae0rjMebwWO6+kF/e9kNsZWc6eTc+V3evWDE+nFkJWaytVvb/yGQ5Pd8i/l52rMSQjtiwpVJWx5VLu9UkjLw3LqUWq/og7rLBV5BsRixe2kETd1BLemPm/SWlwTLYKnaG62jviovh+AmehuMTMg6+4d1RZATkDrckYjEaKUbyVsC4/bFbSCH7g1M4CnEEtu7U0sauXXdNGzMBchQ8Xwgbg2pwwtx/FtLRLLfDEmwn592I7DPVEIW0NE96CZ7GFUCOSVPAwPBuG27Su8fY0qofuzaW5FsTDNOUanMdVwBhwIDAQAB";
    // 日志记录目录
    public static String log_path = "/log";
    // RSA2
    public static String SIGNTYPE = "RSA2";

    public String pay(PayVo payVo){
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL, APPID, RSA_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVo.getOut_trader_no());
        model.setSubject(payVo.getSubject());
        model.setTotalAmount(payVo.getTotal_amount());
        model.setBody(payVo.getBody());
        model.setTimeoutExpress("3000");
        model.setProductCode("1111");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;

        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  null;
    }
}
